package com.alinesno.cloud.base.logger.consumer;

import java.util.Date;
import java.util.Optional;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.alinesno.cloud.base.logger.entity.LogRequestUrlEntity;
import com.alinesno.cloud.base.logger.service.ILogRequestUrlService;
import com.alinesno.cloud.monitor.agent.aspect.BaseAspect;

/**
 * 页面请求链接 
 * 
 * @author WeiXiaoJin
 * @since 2020年1月25日 下午19:45:08
 */
@Component
class MontorontrollerConsumerListener extends BaseConsumer {
	
	private static final Logger log = LoggerFactory.getLogger(MontorontrollerConsumerListener.class) ; 

	@Autowired
	private ILogRequestUrlService logRequestUrlService ; 
	
	/**
	 * 方法消息
	 * @param record
	 * @param data
	 * @param partition
	 * @param topic
	 * @param ts
	 */
	@KafkaListener(topics = { BaseAspect.WATCHER_TOPIC_WEB_CONTROLLER})
	public void listenLogger(ConsumerRecord<?, ?> record, @Payload String data,
			@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
			@Header(KafkaHeaders.RECEIVED_TOPIC) String topic, @Header(KafkaHeaders.RECEIVED_TIMESTAMP) long ts) {

		Optional<?> kafkaMessage = Optional.ofNullable(record.value());
		if (kafkaMessage.isPresent()) {
			
			Object message = kafkaMessage.get();
			log.info("---------------->>>>  message =topic：" + topic + ", " + message);
		
			if (message != null) {
				buildStorageMessage(message + "");
			}
		}
	}

	private void buildStorageMessage(String string) {
		LogRequestUrlEntity e = new LogRequestUrlEntity() ; 

//		"agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
//	    "createTime":0,
//	    "ip":"127.0.0.1",
//	    "method":"com.alinesno.cloud.common.web.basic.auth.controller.HttpBasicDashboardController:home",
//	    "methodDesc":"[Ljava.lang.String;@5fbdb71a",
//	    "methodTime":1027,
//	    "params":"  mmap: {server=com.alinesno.cloud.common.core.monitor.Server@65dbbd4b}",
//	    "url":"http://localhost:30002/dashboard/home"
		
		JSONObject json = JSONObject.parseObject(string) ; 
		
		String ip = json.getString("ip") ; 
		String hostname = json.getString(HOSTNAME) ; 
		String  applicationName = json.getString(APPLICATIONNAME) ; 
		
		String agent = json.getString("agent") ; 
		String method = json.getString("method") ; 
		String methodDesc = json.getString("methodDesc") ; 
		long methodTime = json.getLongValue("methodTime") ; 
		String params = json.getString("params") ; 
		String url = json.getString("url") ; 
		
		e.setIp(ip);
		e.setApplicationName(applicationName);
		e.setHostname(hostname);
		e.setAgent(agent);
		e.setMethod(method);
		e.setMethodDesc(methodDesc);
		e.setMethodTime(methodTime);
		e.setParams(params);
		e.setUrl(url); 
		
		e.setAddTime(new Date());
		logRequestUrlService.save(e) ;
	}
	
}
